<?php
// +----------------------------------------------------------------------
// | 有小象汇 自研正版源码 赋能开发者
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 http://www.uxphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 有小象汇并不是自由软件，未经许可不能去掉有小象汇相关版权
// +----------------------------------------------------------------------
// | Author:  有小象汇
// +----------------------------------------------------------------------

namespace app\adminapi\controller\v1\car;


use app\adminapi\controller\AuthController;
use app\services\car\BaseCityFeeServices;
use think\facade\App;

/**
 * Class BaseCityFee
 * @package app\adminapi\controller\v1\shendeng
 */
class BaseCityFee extends AuthController
{
    public function __construct(App $app,BaseCityFeeServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 列表
     * @return \think\Response
     */
    public function getList(){
        $where = $this->request->getMore([
            ['start_city_id', ''],
            ['end_city_id', ''],
            ['city_fee', ''],
        ]);
        return app('json')->success($this->services->getList($where,true));
    }

    /**
     * 添加/修改
     * @return mixed
     * @throws \FormBuilder\Exception\FormBuilderException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        return app('json')->success($this->services->add((int)$data['id']));
    }

    /**
     * 保存
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save(){
        $data = $this->request->postMore([
            ['id', 0],
            ['start_city_id', ''],
            ['end_city_id', ''],
            ['city_fee', ''],
            ['city_hour', ''],
        ]);
        if (!$data['start_city_id']) return app('json')->fail('请选择取车城市');
        if (!$data['end_city_id']) return app('json')->fail('请选择还车城市');
        if (!$data['city_fee']) return app('json')->fail('请输入还车费用');
        $this->services->save((int)$data['id'], $data);
        return app('json')->success('提交成功');
    }

    /**
     * 删除
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(){
        $data = $this->request->getMore([
            ['id', 0],
        ]);
        if (!$data['id']) return app('json')->fail('参数错误');
        return app('json')->success($this->services->delCityFee((int)$data['id']));
    }
}
